<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use think\Cookie;
use think\Hook;
use think\Session;

/**
 * 会员接口
 */
class Login extends Api
{
    protected $noNeedLogin = ['login', 'register'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();

        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
		/* //监听注册登录退出的事件
		Hook::add('user_login_successed', function ($user) use ($auth) {
		    $expire = input('post.keeplogin') ? 30 * 86400 : 0;
		    Cookie::set('uid', $user->id, $expire);
		    Cookie::set('token', $auth->getToken(), $expire);
		});
		Hook::add('user_register_successed', function ($user) use ($auth) {
		    Cookie::set('uid', $user->id);
		    Cookie::set('token', $auth->getToken());
		}); */

    }
	
	public function login(){
		if ($this->auth->id) {
		    $this->success(__('You\'ve logged in, do not login again'));
		}
		if ($this->request->isPost()) {
		    $account = $this->request->post('account');
		    $password = $this->request->post('password', '', null);
		    $keeplogin = (int)$this->request->post('keeplogin');
		    $token = $this->request->post('__token__');
		    $rule = [
		        'account'   => 'require|length:3,50',
		        'password'  => 'require|length:6,30',
		        '__token__' => 'require|token',
		    ];
		
		    $msg = [
		        'account.require'  => 'Account can not be empty',
		        'account.length'   => 'Account must be 3 to 50 characters',
		        'password.require' => 'Password can not be empty',
		        'password.length'  => 'Password must be 6 to 30 characters',
		    ];
		    $data = [
		        'account'   => $account,
		        'password'  => $password,
		        '__token__' => $token,
		    ];
		    $validate = new Validate($rule, $msg);
		    $result = $validate->check($data);
		    if (!$result) {
		        $this->error(__($validate->getError()), ['token' => $this->request->token()]);
		    }
		    if ($this->auth->login($account, $password)) {
				$expire = input('post.keeplogin') ? 30 * 86400 : 0;
				Cookie::set('uid', $this->auth->id, $expire);
				Cookie::set('token', $this->auth->getToken(), $expire);
				
		        $this->success(__('Logged in successful'));
		    } else {
		        $this->error($this->auth->getError(), ['token' => $this->request->token()]);
		    }
		}
	}

    public function register(){
		
		if ($this->auth->id) {
		    $this->error(__('You\'ve logged in, do not login again'));
		}
		if ($this->request->isPost()) {
		    $username = $this->request->post('username');
		    $password = $this->request->post('password', '', null);
		    $email = $this->request->post('email');
		    $mobile = $this->request->post('mobile', '');
		    $captcha = $this->request->post('captcha');
		    $token = $this->request->post('__token__');
		    $rule = [
		        'username'  => 'require|length:3,30',
		        'password'  => 'require|length:6,30',
		        'email'     => 'require|email',
		        'mobile'    => 'regex:/^1\d{10}$/',
		        '__token__' => 'require|token',
		    ];
		
		    $msg = [
		        'username.require' => 'Username can not be empty',
		        'username.length'  => 'Username must be 3 to 30 characters',
		        'password.require' => 'Password can not be empty',
		        'password.length'  => 'Password must be 6 to 30 characters',
		        'email'            => 'Email is incorrect',
		        'mobile'           => 'Mobile is incorrect',
		    ];
		    $data = [
		        'username'  => $username,
		        'password'  => $password,
		        'email'     => $email,
		        'mobile'    => $mobile,
		        '__token__' => $token,
		    ];
		    //验证码
		    $captchaResult = true;
		    $captchaType = config("fastadmin.user_register_captcha");
		    if ($captchaType) {
		        if ($captchaType == 'mobile') {
		            $captchaResult = Sms::check($mobile, $captcha, 'register');
		        } elseif ($captchaType == 'email') {
		            $captchaResult = Ems::check($email, $captcha, 'register');
		        } elseif ($captchaType == 'wechat') {
		            $captchaResult = WechatCaptcha::check($captcha, 'register');
		        } elseif ($captchaType == 'text') {
		            $captchaResult = \think\Validate::is($captcha, 'captcha');
		        }
		    }
		    if (!$captchaResult) {
		        $this->error(__('Captcha is incorrect'));
		    }
		    $validate = new Validate($rule, $msg);
		    $result = $validate->check($data);
		    if (!$result) {
		        $this->error(__($validate->getError()),['token' => $this->request->token()]);
		    }
		    if ($this->auth->register($username, $password, $email, $mobile)) {
				Cookie::set('uid', $this->auth->id);
				Cookie::set('token', $this->auth->getToken());
		        $this->success(__('Sign up successful'));
		    } else {
		        $this->error($this->auth->getError(),['token' => $this->request->token()]);
		    }
		}
		
	}
}
